﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;

namespace Editor
{
    internal partial class EditorWindow : Form
    {
        public enum EditType
        {
            Select,
            Move,
            Rotate,
            Scale,
        };

        private enum DragType
        {
            None,
            RotateObject,
            RotateCameraAboutCamera,
            RotateCameraAboutTarget,
            TranslateObject,
            TranslateCamera,
        };

        #region Members
        private EditorDocument m_Document;

        private List<DirectXView> m_Views = new List<DirectXView>();

        private DirectXView m_ActiveView;
        private List<Entity> m_SelectedEntities = new List<Entity>();

        private EditType m_EditType = EditType.Select;

        private Point m_ViewMouseDownLocation;
        private Point m_ViewMouseCurrentLocation;
        private bool m_ViewMouseDown = false;
        private bool m_ViewDragging = false;
        private bool m_KeyCtrlDown = false;
        private bool m_KeyAltDown = false;
        private bool m_KeyShiftDown = false;
        private DragType m_DragType = DragType.None;

        private Command.ICommand m_CurrentCommand = null;
        #endregion

        #region Properties
        public EditorDocument Document
        {
            get { return m_Document; }
        }

        public List<DirectXView> Views
        {
            get { return m_Views; }
        }

        public DirectXView ActiveView
        {
            get
            {
                if ((m_ActiveView == null) && (m_Views.Count > 0))
                {
                    m_ActiveView = m_Views[0];
                }

                return m_ActiveView;
            }
            set { m_ActiveView = value; }
        }
        #endregion

        #region Constructor
        public EditorWindow()
        {
            InitializeComponent();

            InitializeEditModes();
            ctrl_ToolStripTabControl.TabControl.SelectedIndexChanged += new EventHandler(OnActiveEditModeChanged);
            OnActiveEditModeChanged();

            ToolStripManager.RenderMode = ToolStripManagerRenderMode.Professional;

            //
            string nsName = typeof(EditorWindow).Namespace;
            this.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream(nsName + ".Resources.Editor.ico"));

            Program.ApplicationIdle += new ApplicationIdleEventHandler(EditorWindow_ApplicationIdle);

            if (RenderUtils.CreateDirect3DDevice(this.Handle))
            {
                SlimDX.Direct3D9.D3DX.DebugMute(false);
            }

            PhysicsUtils.CreatePhysX();

            CreateNewDocument();
        }

        void RegisterViews(Control.ControlCollection controls)
        {
            if (controls == null)
                return;

            foreach (Control control in controls)
            {
                if (control is DirectXView)
                {
                    DirectXView dxView = control as DirectXView;
                    if (!m_Views.Contains(dxView))
                        m_Views.Add(dxView);

                    dxView.InitializeView(m_Document);
                }
                else
                {
                    RegisterViews(control.Controls);
                }
            }
        }
        #endregion

        #region Window State
        private void EditorWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            ToolStripManager.SaveSettings(this, "EditorToolBars");
            Properties.Settings.Default.EditorWindowState = this.WindowState;

            if (this.WindowState == FormWindowState.Normal)
            {
                Properties.Settings.Default.EditorWindowSize = this.Size;
                Properties.Settings.Default.EditorWindowLocation = this.Location;
            }
            else
            {
                Properties.Settings.Default.EditorWindowSize = this.RestoreBounds.Size;
                Properties.Settings.Default.EditorWindowLocation = this.RestoreBounds.Location;
            }

            Properties.Settings.Default.Save();
        }

        private void EditorWindow_Load(object sender, EventArgs e)
        {
            ToolStripManager.SaveSettings(this, "EditorToolBarsReset");

            this.Size = Properties.Settings.Default.EditorWindowSize;
            this.Location = Properties.Settings.Default.EditorWindowLocation;
            this.WindowState = Properties.Settings.Default.EditorWindowState;
            ToolStripManager.LoadSettings(this, "EditorToolBars");
        }
        #endregion

        #region Idle Loop
        private void EditorWindow_ApplicationIdle(float elapsedTime)
        {
            m_Document.Update(elapsedTime);

			if (ActiveEditMode != null)
			{
				ActiveEditMode.OnUpdate(elapsedTime);
			}

            // Don't update the views if they are currently being resized
            // by the user, as it will mess up/clear the drag lines
            if (!ctrl_ViewportLayout.IsSplitterMoving)
            {
                foreach (DirectXView dxView in m_Views)
                {
                    bool bActive = (dxView == ActiveView);
                    bool bDragging = (bActive && m_ViewMouseDown && m_ViewDragging);

                    dxView.Render(elapsedTime, bActive, bDragging);

					if (ActiveEditMode != null)
					{
						ActiveEditMode.OnRender(elapsedTime, dxView, bActive);
					}
                }
            }
        }
        #endregion
    }
}
